package com.seven.springboot.util;

public class UnicodeUtil {

	public static String unicodetoString(String unicode){  
		if(unicode==null||"".equals(unicode)){
			return null;
		}
		StringBuilder sb = new StringBuilder();  
		int i = -1;  
		int pos = 0;  
		while((i=unicode.indexOf("\\u", pos)) != -1){  
			sb.append(unicode.substring(pos, i));  
			if(i+5 < unicode.length()){  
				pos = i+6;  
				sb.append((char)Integer.parseInt(unicode.substring(i+2, i+6), 16));  
			}  
		}  
		return sb.toString();  
	} 
	public static String stringtoUnicode(String string) {
		if(string==null||"".equals(string)){
			return null;
		}
		StringBuffer unicode = new StringBuffer();
		for (int i = 0; i < string.length(); i++) {
			char c = string.charAt(i);
			unicode.append("\\u" + Integer.toHexString(c));
		}
		return unicode.toString();
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String s = stringtoUnicode("中文");
		System.out.println("编码："+s);
		String s1 = unicodetoString(s);
		System.out.println("解码："+s1);
		String str = "{\\\"result\\\":\\\"00000000\\\",\\\"message\\\":\\\"\\U6210\\U529f\\\",\\\"data\\\":{\\\"Cid\\\":\\\"d27ab5d1-ef7e-4816-9499-6062d6f6ab92\\\",\\\"LogName\\\":\\\"18810802899\\\",\\\"Password\\\":\\\"C51CD8E64B0AEB778364765013DF9EBE\\\",\\\"NickName\\\":\\\"ly\\\",\\\"Status\\\":\\\"1\\\",\\\"EMail\\\":null,\\\"PersonalSignature\\\":null,\\\"Hobby\\\":null,\\\"ImagePath\\\":null,\\\"Sex\\\":null,\\\"Birth\\\":null,\\\"Mobile\\\":\\\"18810802899\\\",\\\"IsEnabled\\\":\\\"1\\\",\\\"RegDate\\\":\\\"2018-08-21T10:38:13.12\\\",\\\"UpdateNullFields\\\":null}}";
		System.out.println("a");
		System.out.println(unicodetoString(str));
		System.out.println("b");
 
	}
	
}
